<?php
namespace app\admin\controller;
use app\admin\model\Outlink;
use app\admin\model\Outcate;
use app\admin\traits\Result;
use think\facade\Request;
use think\Db;


class Sysconfig extends Common
{
    
    public function outlink(){
        
        if ($this->request->isAjax()) {
            $data = [
                'key' => $this->request->get('key', '', 'trim'),
                'limit' => $this->request->get('limit', 10, 'intval'),
            ];
            
            $list = Outlink::withSearch(['title'], ['title' => $data['key']])->order('id desc')
                ->paginate($data['limit'], false, ['query' => $data]);
            $user_date = [];
            foreach ($list as $key => $val) {
                $user_date[$key] = $val;
            }
            return show($user_date, 0, '', ['count' => $list->total()]);
        }
        return $this->fetch();
        
    }
    
    //验证是否存在
    public function checkoutlink()
    {
        $username = $this->request->get('title', '', 'trim');
        $res = Outlink::where('title', '=', $username)->field('title')->find();
        if ($res) {
            $msg = ['code' => 0, 'msg' => '名称已存在'];
        } else {
            $msg = ['status' =>1, 'info' => '验证通过'];
        }
        return $msg;
    }
    
    public function addoutlink(){
       if ($this->request->isPost()) {
            $data = $this->request->post();

            $validate = validate('Outlink');
            if (!$validate->check($data)) {
               //令牌数据无效时重置令牌
               $validate->getError() != '令牌数据无效' ? $token = Request::token() : $token = '';
               $msg = Result::error($validate->getError(), null, ['token' => $token]);
               return $msg;
            }

            if ($data['id']) {
                //编辑
                $res = Outlink::update($data, ['id' => $data['id']]);
                if ($res) {
                    $msg = Result::success('编辑成功', url('/admin/sysconfig-outlink'));
                } else {
                    $msg = Result::error('编辑失败');
                }
            } else {
                //添加
                $outlink = new Outlink;
                $res = $outlink->allowField(true)->save($data);
                if ($res) {
                    $msg = Result::success('添加成功', url('/admin/sysconfig-outlink'));
                } else {
                    $msg = Result::error('添加失败');
                }
            }
            return $msg;
        } else { 
            $id = $this->request->get('id', 0, 'intval');
            if ($id) {
                $list = Outlink::where('id', '=', $id)->find();
               
                $this->assign('list', $list);
            }
			
			$cate_list = Db::name('outcate')->select();
			$this->assign('cate_list', $cate_list);
            return $this->fetch();
        } 
        
    }
    public function deloutlink(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
                $res = Outlink::where('id='.$id)->delete();
                if ($res) {
                    $msg = Result::success('删除成功');
                } else {
                    $msg = Result::error('删除失败');
                }
        
                return $msg;
            
        } else {
            $this->error('参数错误');
        }
        
    }
	
	public function outcate(){
        
        if ($this->request->isAjax()) {
            $data = [
                'key' => $this->request->get('key', '', 'trim'),
                'limit' => $this->request->get('limit', 10, 'intval'),
            ];
            
            $list = Outcate::withSearch(['title'], ['title' => $data['key']])->order('id desc')
                ->paginate($data['limit'], false, ['query' => $data]);
            $user_date = [];
            foreach ($list as $key => $val) {
                $user_date[$key] = $val;
            }
            return show($user_date, 0, '', ['count' => $list->total()]);
        }
        return $this->fetch();
        
    }
    
    public function checkoutcate()
    {
        $username = $this->request->get('title', '', 'trim');
        $res = Outcate::where('title', '=', $username)->field('title')->find();
        if ($res) {
            $msg = ['code' => 0, 'msg' => '名称已存在'];
        } else {
            $msg = ['status' =>1, 'info' => '验证通过'];
        }
        return $msg;
    } 
    
    public function addoutcate(){
       if ($this->request->isPost()) {
            $data = $this->request->post();

            $validate = validate('Outcate');
            if (!$validate->check($data)) {
               //令牌数据无效时重置令牌
               $validate->getError() != '令牌数据无效' ? $token = Request::token() : $token = '';
               $msg = Result::error($validate->getError(), null, ['token' => $token]);
               return $msg;
            }

            if ($data['id']) {
                //编辑
                $res = Outcate::update($data, ['id' => $data['id']]);
                if ($res) {
                    $msg = Result::success('编辑成功', url('/admin/sysconfig-outcate'));
                } else {
                    $msg = Result::error('编辑失败');
                }
            } else {
                //添加
                $outcate = new Outcate;
                $res = $outcate->allowField(true)->save($data);
                if ($res) {
                    $msg = Result::success('添加成功', url('/admin/sysconfig-outcate'));
                } else {
                    $msg = Result::error('添加失败');
                }
            }
            return $msg;
        } else { 
            $id = $this->request->get('id', 0, 'intval');
            if ($id) {
                $list = Outcate::where('id', '=', $id)->find();
               
                $this->assign('list', $list);
            }
			
            return $this->fetch();
        } 
        
    }
    public function deloutcate(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
                $res = Outcate::where('id='.$id)->delete();
                if ($res) {
                    $msg = Result::success('删除成功');
                } else {
                    $msg = Result::error('删除失败');
                }
        
                return $msg;
            
        } else {
            $this->error('参数错误');
        }
        
    }

}



?>